package info.maxhis.bn.birthday;

import info.maxhis.bn.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 根据日期查找生日
 * @author iStar
 *
 */
public class SearchByDateActivity extends Activity {
	private TextView fromView,toView;
	private Button fromButton,toButton,searchButton;
	private Context context;
	private Resources rs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_by_date);
		findViews();
		bindViews();
	}

	private void findViews() {
		context = this;
		rs = getResources();
		fromView = (TextView) findViewById(R.id.view_date_from);
		toView = (TextView) findViewById(R.id.view_date_to);
		fromButton = (Button) findViewById(R.id.btn_date_from);
		toButton = (Button) findViewById(R.id.btn_date_to);
		searchButton = (Button) findViewById(R.id.btn_search);
	}

	private void bindViews() {
		String now = new SimpleDateFormat("MM-dd").format(new Date());
		fromView.setText(now);
		toView.setText(now);
		fromButton.setOnClickListener(new DateChangeListener(fromView));
		toButton.setOnClickListener(new DateChangeListener(toView));
		searchButton.setOnClickListener(new SearchButtonListener());
	}
	
	/**
	 * 查找按钮监听器
	 */
	private class SearchButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String from = fromView.getText().toString();
			String to = toView.getText().toString();
			Intent intent = new Intent(context, BirthdayListActivity.class);
			intent.putExtra("from", from);
			intent.putExtra("to", to);
			startActivity(intent);
		}
		
	}
	/**
	 * 弹出DatePicker设置起始日期
	 * @author iStar
	 *
	 */
	private class DateChangeListener implements OnClickListener {
		private TextView view;

		public DateChangeListener(TextView view) {
			this.view = view;
		}

		@Override
		public void onClick(View v) {

			LayoutInflater inflater = LayoutInflater.from(context);
			LinearLayout layout = (LinearLayout) inflater.inflate(
					R.layout.date_picker, null);
			final DatePicker datePicker = (DatePicker)layout.findViewById(R.id.date_picker);				
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setView(layout);
			String date=view.getText().toString();
	    	if(date!=null && !date.equals("")){		    		
	    		String[] sdte=date.split("-");
	    		datePicker.init(Calendar.getInstance().get(Calendar.YEAR),Integer.parseInt(sdte[0])-1,Integer.parseInt(sdte[1]),null);
	    	}
	    	dialog.setIcon(R.drawable.view).setTitle(rs.getString(R.string.date_set));
			dialog.setPositiveButton(rs.getString(R.string.setting),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int whichButton) {
						String strDamTime=(datePicker.getMonth()+1)+"-"+datePicker.getDayOfMonth();
						view.setText(strDamTime);
					}
				});
			dialog.setNegativeButton(rs.getString(R.string.cancel),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
			});
			dialog.create().show();
		}
	}
}
